package com.axzhengxin.platform.afterloan.service.impl;

import com.axzhengxin.platform.afterloan.domain.RiskModelResult;
import com.axzhengxin.platform.afterloan.mapper.RiskModelResultMapper;
import com.axzhengxin.platform.common.util.SmsSendService;
import com.axzhengxin.platform.sync.ISyncDataService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * RiskModelSyncServiceImpl
 *定时发送短信给预警当前管护人/20210419
 * @author XPS
 * @date 2021/4/21
 */

@Slf4j
@Service("riskModel")
public class RiskModelSyncServiceImpl implements ISyncDataService{

    @Autowired
    private RiskModelResultMapper riskModelResultMapper;

    @Autowired
    private SmsSendService smsSendService;

    @SneakyThrows
    public void exec() {
        //异步发送短信提醒
        log.info("==================定时开始发送短信提醒================");
        List<RiskModelResult> riskModelResults = riskModelResultMapper.selectRiskModelResultSendMsg();
        int num = 0; // 计数发送成功多少笔客户;
        for (RiskModelResult rmr : riskModelResults) {
            String phoneNum = rmr.getPhoneNum();
            String entName = rmr.getEntName();
            String msg = "您的客户" + entName + "有新触发预警信号，登陆企业征信服务中心了解预警详情。";
            try {
                String result = smsSendService.sendMsg("prod", phoneNum, null, msg, "412607");
                if("0".equals(result)){
                    num++;
                    log.info("=====发送短信提醒成功，开始跟新表中发送短信状态======");
                    // 如果发行短信成功记录“是否发送短信”
                    rmr.setIsSendMessage("1");
                    riskModelResultMapper.updateRiskModelResult(rmr);
                }
            } catch (Exception e) {
                log.info("短信发送失败" + msg);
                e.printStackTrace();
            }
        }
        log.info("============定时发送预警短信提醒全部结束==============一共发送:"+num+"笔客户");
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 10;
    }
}
